package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Car;
import cn.wolfcode.crm.mapper.CarMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICarService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CarServiceImpl implements ICarService {
    @Autowired
    private CarMapper carMapper;

    public void saveOrUpdate(Car entity) {
        if (entity.getId() == null) {
            carMapper.insert(entity);
        } else {
            carMapper.updateByPrimaryKey(entity);
        }
    }

    public void delete(Long id) {
        carMapper.deleteByPrimaryKey(id);
    }

    public Car get(Long id) {
        return carMapper.selectByPrimaryKey(id);
    }

    public List<Car> listAll() {
        return carMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = carMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.emptyList());
        }
        List<Car> cars = carMapper.queryList(qo);
        return new PageResult(count, cars);
    }

    @Override
    public Car selectByInsureId(Long insureId) {
        return carMapper.selectByInsureId(insureId);
    }
}
